package com.example.simpdptest;

import java.util.Iterator;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.aaa.R;

/**
 * 此控件是中间带滚动条的布局，不包含上下两个按钮
 * 
 * 郭世杰
 * 
 * @version
 * 
 */
public class FloorScrollBar extends View {

    public final static int TOP=-1;
    public final static int Bottom=-2;
    // 要画的图片
    private Drawable indoor_blue;
    private Drawable indoor_gray;

    private boolean mIsIndoorEnable = true;// 室内地图是否可用
    // 背景
    private int mBackGroudWidth;
    private int mBackGroudHeight;
    // 滑块
    private int mListviewHeight;
    private int mSeekHeight;
    private int mUpButtonHeight;
    private int marginToItem;
    private int mBarTop; // 滑块离顶部距
//    private int mBarTop_last;// 上次滑块离顶部距
    private OnBarListener mListener;

    private int mNowProgress;// 当前滚动的位置

    /**
     * 回调接口
     * 
     * @version ScrollBarView
     * @since
     */
    public interface OnBarListener {

	void onBarClick();

    }

    public FloorScrollBar(Context context, AttributeSet attrs) {
	super(context, attrs);
	init(context);
    }

    public FloorScrollBar(Context context) {
	super(context);
	init(context);
    }

    private void init(Context context) {
	indoor_blue = context.getResources().getDrawable(
		R.drawable.indoor_myfloor_icon_blue);// 定位可用图片
	indoor_gray = context.getResources().getDrawable(
		R.drawable.indoor_myfloor_icon_gray);// 定位不可用图片
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
	super.onSizeChanged(w, h, oldw, oldh);
	mBackGroudWidth = w;
	mBackGroudHeight = h;
	setWidthHeight();
    }
    
    private int size=4;
    public void IsShowButton(int size){
	this.size=size;
	setWidthHeight();
    }

    /**
     * 控制滚动条、滚动条背景的宽高
     * 
     */
    private void setWidthHeight() {
	mListviewHeight = mBackGroudHeight - dip2px(getContext(), 44);// listview高度
	mUpButtonHeight = dip2px(getContext(), 22);// 上面按钮的高度
	mSeekHeight = dip2px(getContext(), 21);// 滑块的高度

	marginToItem = (mListviewHeight / size - mSeekHeight) / 2;// 在listview上显示的时候，滑块距离item分割线的距离

	// setProgress(1);
    }

    @Override
    protected void onDraw(Canvas canvas) {
	Paint paint = new Paint();
	// paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
	// canvas.drawPaint(paint);
	// paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
	Bitmap buffer = Bitmap.createBitmap(mBackGroudWidth, mBackGroudHeight,
		Bitmap.Config.ARGB_4444);
	buffer.eraseColor(Color.TRANSPARENT);
	canvas.drawBitmap(buffer, 0, 0, paint);

	if (mIsIndoorEnable) {
	    indoor_blue.draw(canvas);
	} else {
	    indoor_gray.draw(canvas);
	}
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

	if (event.getY() > mBarTop && event.getY() < mBarTop + mSeekHeight) {
	    if (mListener != null) {
		mListener.onBarClick();
	    }
	    return true;
	}

	return super.onTouchEvent(event);

    }

    /**
     * 获得当前滚动的位置
     * 
     * @param @return 
     * @author zhuanggy
     * @date 2014-3-28
     */
    public int getNowProgress() {
	return this.mNowProgress;
    }

    /**
     * 设置滚动条滚动位置
     * 
     * @param @param progress 当前楼层 progress=-1 代表最定的 progress=-2 代表最低端
     */
    public void setProgress(int progress) {
	// Log.e("", "progress=" +progress);
	mNowProgress = progress;
	if (mNowProgress == TOP) {
	    mBarTop = 0;
	    if (mIsIndoorEnable) {
		indoor_blue.setBounds(0, mBarTop, mSeekHeight, mBarTop
			+ mSeekHeight);
	    } else {
		indoor_gray.setBounds(0, mBarTop, mSeekHeight, mBarTop
			+ mSeekHeight);
	    }
	    invalidate();
//	    mBarTop_last = mBarTop;
	} else if (mNowProgress == Bottom) {
	    mBarTop = mBackGroudHeight - mSeekHeight;
	    if (mIsIndoorEnable) {
		indoor_blue.setBounds(0, mBarTop, mSeekHeight, mBarTop
			+ mSeekHeight);
	    } else {
		indoor_gray.setBounds(0, mBarTop, mSeekHeight, mBarTop
			+ mSeekHeight);
	    }
	    invalidate();
//	    mBarTop_last = mBarTop;
	} else {
//	    System.out.println("mUpButtonHeight="+mUpButtonHeight);
//	    System.out.println("marginToItem="+marginToItem);
	    mBarTop = mUpButtonHeight + (progress * mListviewHeight / size)
		    + marginToItem;
	    handler.sendEmptyMessage(0);

	}
    }
    
    

    private Handler handler = new Handler() {

	@Override
	public void handleMessage(Message msg) {
	    if (msg.what == 0) {
		if (mIsIndoorEnable) {
		    indoor_blue.setBounds(0, mBarTop, mSeekHeight, mBarTop
			    + mSeekHeight);
		} else {
		    indoor_gray.setBounds(0, mBarTop, mSeekHeight, mBarTop
			    + mSeekHeight);
		}
		invalidate();
	    }

	    super.handleMessage(msg);

	}

    };

    public boolean ismIsIndoorEnable() {
	return mIsIndoorEnable;
    }

    public void setmIsIndoorEnable(boolean mIsIndoorEnable) {
	this.mIsIndoorEnable = mIsIndoorEnable;
	invalidate();
    }

    /**
     * 设置滚动条监听
     * 
     * @param @param a_Listen 
     * @date 2014-3-26
     */
    public void setListener(OnBarListener a_Listen) {
	mListener = a_Listen;
    }

    public static int dip2px(Context context, float dipValue) {
	final float scale = context.getResources().getDisplayMetrics().density;
	return (int) (dipValue * scale + 0.5f);
    }
}
